﻿/*
	Class			Pixel
	
	Description		Draws red, green, and blue phosphors based on a given color value: $pixelValue.
					It also draws a black line gap at the bottom.
					Pixel size can be changed with value: $pixelWidth.
					
	History			1.04	7/09/07		Changed colors to uints and Allows Alpha values for the pixels;
					1.03	6/18/07		Changed all uints to ints.
					1.02	5/31/07		Changed the class to extend Shape instead of Sprite for performance benefits.
					1.01	5/29/07		Uses a gradient fill on 1 shape, instead of solid fills on 3 shapes.
					1.00	5/26/07		Created
	
	To Do			change color numbers to uints
*/

package tv.digitalfx
{
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.display.Graphics;
	import flash.display.GradientType;
	import flash.geom.Matrix;
	
	public class Pixel extends Shape
	{
		public  var red:uint;
		public  var green:uint;
		public  var blue:uint;
		private var _alpha:Number;
		private var _pixelValue:uint;
		private var _pixelWidth:int;
		private var _gapOn:Boolean;
		private var _gapStroke:int;
		private var _phosphorWidth:int;
		private var _phosphorHeight:int;
		
		
		public function Pixel ($pixelValue:uint = 0x00FFFFFF, $pixelWidth:int = 3, $gapOn:Boolean = false, $gapStroke:int = 1)
		{
			_pixelValue     = $pixelValue;
			_pixelWidth     = $pixelWidth;
			_gapOn          = $gapOn;
			_gapStroke      = $gapStroke;
			_phosphorWidth  = _pixelWidth / 3;
			_phosphorHeight = _pixelWidth;
			
			if (_gapOn)
				_phosphorHeight = _pixelWidth - _gapStroke;
			
			_alpha        = (_pixelValue >> 24 & 0xff) / 255;
			this.red      = (_pixelValue >> 16) << 16;
			this.green    = (_pixelValue >> 8 & 0xff) << 8;
			this.blue     = _pixelValue & 0xff;
			
			this.createPhosphors ();
			
			if (_gapOn)
				this.createPixelGap ();
		}
		
		
		private function createPhosphors ():void
		{
			var colors:Array  = [red,    red,    green,  green,  blue,   blue  ];
			var alphas:Array  = [_alpha, _alpha, _alpha, _alpha, _alpha, _alpha];
			var ratios:Array  = [0,      84,     86,     169,    171,    255   ]; // Seperates Gradients to Thirds: 0, 85, 170 (42, 127, 213)
			var matrix:Matrix = new Matrix ();
			
			matrix.createGradientBox   (_pixelWidth, _phosphorHeight);
			graphics.beginGradientFill (GradientType.LINEAR, colors, alphas, ratios, matrix);
			graphics.drawRect          (0, 0, _pixelWidth, _phosphorHeight);
		}
		
		
		private function createPixelGap ():void
		{
			graphics.beginFill (0x000000);
			graphics.drawRect  (0, _phosphorHeight, _pixelWidth, _gapStroke);
		}
		
	}
}


